package com.chenzouquan.view;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class UploadFileUI extends JFrame implements ActionListener {
    private JTextField filenameField;
    private JButton browseButton;
    private JButton uploadButton;
    private JButton cancelButton;
    private JButton clearButton;
    private JTextArea logArea;

    public UploadFileUI() {
        super("文件上传");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 300);

        // 创建文件名文本框和浏览按钮
        filenameField = new JTextField();
        browseButton = new JButton("浏览");
        browseButton.addActionListener(this);

        // 创建上传、取消和清除按钮
        uploadButton = new JButton("上传");
        uploadButton.addActionListener(this);
        cancelButton = new JButton("取消");
        cancelButton.addActionListener(this);
        clearButton = new JButton("清除");
        clearButton.addActionListener(this);

        // 创建日志区域
        logArea = new JTextArea();
        logArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(logArea);

        // 创建布局
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        topPanel.add(filenameField, BorderLayout.CENTER);
        topPanel.add(browseButton, BorderLayout.EAST);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(uploadButton);
        buttonPanel.add(cancelButton);
        buttonPanel.add(clearButton);

        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(topPanel, BorderLayout.NORTH);
        contentPane.add(buttonPanel, BorderLayout.CENTER);
        contentPane.add(scrollPane, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == browseButton) {
            JFileChooser chooser = new JFileChooser();
            int result = chooser.showOpenDialog(this);
            if (result == JFileChooser.APPROVE_OPTION) {
                String filename = chooser.getSelectedFile().getPath();
                filenameField.setText(filename);
                logArea.append("选择了文件: " + filename + "\n");
            }
        } else if (e.getSource() == uploadButton) {
            String filename = filenameField.getText();
            if (filename.equals("")) {
                JOptionPane.showMessageDialog(this, "请选择文件", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }
            // 在此添加上传文件的代码
            logArea.append("上传了文件: " + filename + "\n");
        } else if (e.getSource() == cancelButton) {
            dispose();
        } else if (e.getSource() == clearButton) {
            logArea.setText("");
        }
    }

    public static void main(String[] args) {
        UploadFileUI ui = new UploadFileUI();
        ui.setVisible(true);
    }
}

